import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import "../component" as C
import "qrc:/component/ZGlobal.js" as Global

C.ZModelRect {
    id: rootWeightCalibrationPage
    radius: 20
    clip: true
    anchors.fill: parent
    visible: false
    color: "transparent"
    property int status:0
    property int count: 10

    function resetForm(){
        visible=true;
        status=0;
        count=10;
        msg.text = "";
    }

    onVisibleChanged: {
        collectionInfo.setCollect(!visible);
    }

    Image {
        x:646;y:280;width:778;height:520;
        source: "qrc:/assets/weight_calibration_bg.png"

        Image {
            x: 687-parent.x
            y: 319-parent.y
            width: 16
            height: 32
            source: "qrc:/assets/greenrect.png"
        }

        Label{
            x:68
            y:36
            Text{
                text: "称重校准"
                font.pixelSize: 30
                color: "#191A1A"
                font.bold: true
            }
        }

        Image {
            id: closeBtn
            x: 1358-parent.x
            y: 317-parent.y
            width: 24
            height: 24
            property bool pressed: false
            scale: pressed ? 0.90 : 1
            source: "qrc:/assets/close.png"
            MouseArea{
                anchors.fill: parent
                onPressed: closeBtn.pressed=true
                onReleased: closeBtn.pressed=false
                onClicked: {
                    console.log("close")
                    rootWeightCalibrationPage.visible=false
                }
            }
        }

        Image {
            x: 756-parent.x
            y: 398-parent.y
            width: 52
            height: 48
            source: "qrc:/assets/step1.png"
        }

        Image {
            id: step2Img
            x: 1009-parent.x
            y: 398-parent.y
            width: 53
            height: 48
            source: rootWeightCalibrationPage.status<2 ? "qrc:/assets/step2.png":"qrc:/assets/step2_colorful.png"
        }

        Image {
            id: step3Img
            x: 1263-parent.x
            y: 398-parent.y
            width: 53
            height: 48
            source: rootWeightCalibrationPage.status>3 ? "qrc:/assets/step3_colorful.png":"qrc:/assets/step3.png"
        }
        Label{
            x:741-parent.x
            y:452-parent.y
            Text{
                text: "零点校准"
                font.pixelSize: 22
                color: "#178FCD"
            }
        }
        Label{
            x:995-parent.x
            y:452-parent.y
            Text{
                text: "砝码标定"
                font.pixelSize: 22
                color: "#A7AAB3"
            }
        }

        Label{
            x:1267-parent.x
            y:452-parent.y
            Text{
                text: "完成"
                font.pixelSize: 22
                color: "#A7AAB3"
            }
        }

        Label{
            x:0
            y: 557 - parent.y
            width: parent.width
            horizontalAlignment: Text.AlignHCenter  // 添加这行
            Text {
                text: count
                font.pixelSize: 35
                font.bold: true;
                horizontalAlignment: Text.AlignHCenter
                color: "#178FCD"
                anchors.fill: parent  // 让Text填满整个Label
            }
            visible: rootWeightCalibrationPage.status == 1 ||rootWeightCalibrationPage.status == 3
        }

        Label{
            x:0
            y: 650 - parent.y
            width: parent.width
            horizontalAlignment: Text.AlignHCenter  // 添加这行
            Text {
                id: msg
                text: ""
                font.pixelSize: 35
                horizontalAlignment: Text.AlignHCenter
                color: "red"
                anchors.fill: parent  // 让Text填满整个Label
            }
            visible: msg.text.length > 0
        }

        Image {
            id: imgBtn
            property bool pressed: false
            x: 920-parent.x
            y: 557-parent.y
            width: 232
            height: 72
            scale: pressed ? 0.98 : 1
            source: "qrc:/assets/button_calibration.png"
            visible: rootWeightCalibrationPage.status < 1
            Label{
                x:1010-parent.x-parent.parent.x
                y:580-parent.y-parent.parent.y
                Text{
                    text: "校准"
                    font.pixelSize: 22
                    color: "#FFFFFF"
                }
            }
            MouseArea{
                anchors.fill: parent
                onPressed: imgBtn.pressed=true
                onReleased: imgBtn.pressed=false
                onClicked: {
                    console.log("校准")
                    collectionInfo.calibration0();
                    countdownTimer.start();
                    rootWeightCalibrationPage.status++;
                    descText.text="注：清空XX内物料，确保校准过程中，不受外部干扰！\n待计数稳定后，先输入数值，再点按钮执行“0点校准”"
                }
            }
        }
        Image {
            id: resultImg
            x: 895-parent.x
            y: 542-parent.y
            width: 60
            height: 60
            source: "qrc:/assets/success.png"
            visible: rootWeightCalibrationPage.status==4
        }

        Label{
            id: resultText
            x:975-parent.x
            y:552-parent.y
            Text{
                text: "校准成功！"
                font.pixelSize: 38
                color: "#178FCD"
            }
            visible: rootWeightCalibrationPage.status==4
        }

        C.ZInputImage{
            id: valueInput
            x:754-parent.x
            y:562-parent.y
            width: 283
            height:63
            placeholderText: "请输入砝码重量"
            validator: IntValidator {
                bottom: 1  // 最小值
                top: 999      // 最大值
            }
            visible: rootWeightCalibrationPage.status ==2
        }
        C.ZButton{
            id: zeroCalibrationBtn
            x:1090-parent.x
            y:560-parent.y
            width:232
            text: "砝码标定"
            color: "#178FCD"
            fontColor: "white"
            visible: rootWeightCalibrationPage.status==2
            onClicked: {
                console.log("校准");
                if (valueInput.text.length===0){
                    msg.text = "请输入砝码重量"
                    return;
                }

                msg.text = "";
                collectionInfo.calibrationWeight(parseInt(valueInput.text));
                rootWeightCalibrationPage.count=10;
                countdownTimer.start()
                rootWeightCalibrationPage.status++
                descText.text="注：请确认校准是否成功！"
            }
        }

        Label{
            x:702-parent.x
            y:715-parent.y
            Text{
                id: descText
                text: "注：清空xx内物料，确保校准过程中，不受外部干扰\n待读数稳定后，点击按钮执行“1点校准”。"
                font.pixelSize: 22
                color: "#6D7278"
            }
        }
    }
    Timer {
        id: countdownTimer
        interval: 1000
        repeat: true
        onTriggered: rootWeightCalibrationPage.count > 0 ? rootWeightCalibrationPage.count-- : rootWeightCalibrationPage.stop()
    }
    function stop(){
        console.log("stop");
        countdownTimer.stop();
        rootWeightCalibrationPage.status++;
    }
}

